<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/19
 * Time: 17:10
 */
namespace Admin\Controller;

use Think\Controller;

class FriendsController extends Controller
{
    private $friend = null;

    public function _initialize()
    {
        $this->friend = D('Friend');
    }

    public function friendsList()
    {
        $friends = $this->friend->order('sort asc')->select();
        $this->assign(array(
            'friends' => $friends
        ));
        $this->display('Friends/list');
    }

    public function friendsAdd()
    {
        if (!IS_POST) {
            $this->display('Friends/add');
            exit;
        }

        if ($this->friend->friendsAdd()) {
            $this->ajaxReturn('true');
        } else {
            $this->ajaxReturn($this->friend->getError());
        }
    }

    public function friendsModify()
    {
        if (!IS_POST) {
            $id = I('get.id');
            $friend = $this->friend->where(array('id'=>array('eq',$id)))->find();
            $this->assign(array(
                'friend'   => $friend
            ));
            $this->display('Friends/edit');
            exit;
        }

        if ($this->friend->friendsModify()) {
            $this->ajaxReturn('true');
        } else {
            $this->ajaxReturn($this->friend->getError());
        }
    }

    public function friendsDel()
    {
        $id = I('get.id');

        $result = $this->friend->where(array('id' => array('eq', $id)))->delete();

        if ($result !== false) {
            $this->ajaxReturn('true');
        } else {
            $this->ajaxReturn($this->friend->getError());
        }
    }

}